package cc.rengu.igas.cops.common.util;

import cc.rengu.oltp.utility.util.StringUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Enumeration;

/**
 * RsaUtil 收银台Rsa操作类
 *
 * @author wukefan
 * @date 2020/06/10
 */
public class RsaUtil {

    /**
     * 从公钥证书文件中获取公钥
     *
     * @param certFilePath 公钥证书文件路径
     * @param storePass    证书库密码
     * @param alias        证书别名    不上送则认为该证书库只有一个证书，取第一个证书别名
     * @return 公钥证书
     * @throws Exception 异常
     */
    public static RSAPublicKey getJksPublicKeyFromCertFile(String certFilePath, String storePass, String alias) {
        File certFile = new File(certFilePath);
        Certificate certificate = null;
        if (certFile.exists()) {
            if (certFilePath.endsWith(".jks")) {
                FileInputStream fileInputStream = null;
                try {
                    fileInputStream = new FileInputStream(certFilePath);

                    KeyStore keyStore = KeyStore.getInstance("JKS");
                    keyStore.load(fileInputStream, storePass.toCharArray());
                    fileInputStream.close();
                    if (StringUtil.isEmptyOrNull(alias)) {
                        Enumeration aliasAll = keyStore.aliases();
                        //不送别名认为keyStore中只有一个
                        if (aliasAll.hasMoreElements()) {
                            alias = (String) aliasAll.nextElement();
                        } else {

                            return null;
                        }
                    }
                    certificate = keyStore.getCertificate(alias);
                } catch (FileNotFoundException | KeyStoreException e) {

                } catch (CertificateException e) {

                } catch (NoSuchAlgorithmException e) {

                } catch (IOException e) {

                } finally {
                    if (null != fileInputStream) {
                        try {
                            fileInputStream.close();
                        } catch (IOException e) {
                        }
                    }
                }
                return (RSAPublicKey) certificate.getPublicKey();
            } else {

                return null;
            }
        } else {

            return null;
        }
    }


    /**
     * 从证书文件中获取私钥
     *
     * @param certFilePath 证书文件路径
     * @param storePass    证书库密码
     * @param alias        证书别名
     * @param certPass     证书密码
     * @return 私钥信息
     * @throws Exception 异常
     */
    public static RSAPrivateKey getJksPrivateKeyFromCertFile(String certFilePath, String storePass, String alias, String certPass) {
        File certFile = new File(certFilePath);
        if (certFile.exists()) {
            FileInputStream fileInputStream = null;
            try {
                KeyStore keyStore = KeyStore.getInstance("JKS");
                fileInputStream = new FileInputStream(certFilePath);
                keyStore.load(fileInputStream, storePass.toCharArray());
                fileInputStream.close();
                if (StringUtil.isEmptyOrNull(alias)) {
                    Enumeration aliasAll = keyStore.aliases();
                    //不送别名认为keyStore中只有一个
                    if (aliasAll.hasMoreElements()) {
                        alias = (String) aliasAll.nextElement();
                    } else {
                        return null;
                    }
                }
                return (RSAPrivateKey) keyStore.getKey(alias, certPass.toCharArray());
            } catch (Exception e) {

            } finally {
                if (null != fileInputStream) {
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                    }
                }
                return null;
            }
        } else {
            return null;
        }
    }

}
